<?php
/**
 * Quản lý vai trò
 *
 * @version $Rev:$
 * @license
 */
class Admin_RolesController extends Zend_Controller_Action
{
    public function init()
    {
        $this->params = $this->getRequest()->getParams();
        System_Template::using("blank","Admin");
        $this->model = new Admin_Model_Roles();
        parent::init();
    }

    public function indexAction()
    {
        $table = new System_Table_Roles();
        $rs = $table->fetchAll();
        $sD = config::getSourcesDescription();
        $data = $sD->module->toArray();
        /*
        foreach($data as $item){
            if($item['description'] !=""){
                //var_dump($item['controller']);
                echo '<h1>'.$item['description'].'</h1>';
                 $keys = array_keys($item['controller']);
                 for($i=0;$i<count($keys);$i++)
                 {
                        $description = $item['controller'][$keys[$i]]['description'];
                        echo '<h2> &nbsp;&nbsp;&nbsp;&nbsp;'.$keys[$i].' : '.$description.'</h2>';
                        $arr_action = $item['controller'][$keys[$i]]['action'];
                        $keyAction = array_keys($arr_action);
                        for($ii=0;$ii<count($keyAction);$ii++)
                        {
                            echo '<h3> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'.$keyAction[$ii].' : '.$arr_action[$keyAction[$ii]].'</h3>';
                        }
                 }
            }
        }*/
        
        $this->view->data = $rs ;
        $this->view->dataSD = $sD ;
        $this->view->dataSources = $data; 
        common::showButton('delete');
        common::showButton('add');
    }

    public function inputAction()
    {
        common::showButton('save');
        common::showButton('back');
        $code = $this->params['CODE'];
        $sD = config::getSourcesDescription();
        $dataSources = $sD->module->toArray();
        if($this->params['CODE'] != ''){
            $table = new System_Table_Roles();
            $select = $table->select()
                                ->from($table,array('CODE','NAME','STATUS','NAVIGATION'))
                                ->where('CODE = ?',$code);
            $data = $table->fetchAll($select);
            $this->view->data = $data->current();
        }
        $this->view->dataSources = $dataSources; 
    }
    
    
    /**
    * Lưu định hướng truy cập
    */
    public function changenavigationAction()
    {
        try{
            $table = new System_Table_Roles();
            $arr = array(
                'NAVIGATION' => $this->params['URLNAV']
            );
            $where = $table->getAdapter()->quoteInto("CODE = ? ", $this->params['CODENAV']);
            $table->update($arr,$where);
            ajax::ship('rsChangeNav',1);
        }catch(Exception $ex ){
            ajax::ship('rsChangeNav',0);
            ajax::ship('rsChangeNavError',$ex->getMessage());
        }
        exit;
    }
    public function saveAction()
    {
        $table = new System_Table_Roles();
        $count = '0';
        $status = '0';
        if(isset($this->params['STATUS']))
        {
            $status = '1';
        }
        if($this->params['CODE'] != '' && $this->params['NAME'] != ''){
            $arr = array(
                'NAME'=> $this->params['NAME'],
                'CODE'=> $this->params['CODE'],
                'NAVIGATION'=> $this->params['NAVIGATION'],
                'STATUS'=>$status
            );

            if($this->params['ISUPDATE'] != '' ){
                $oldCode = $this->params['CODEOLD'];
                $newCode = $arr['CODE'];
                
                if($oldCode != $newCode && $oldCode != ""){
                    config::replaceRoles($oldCode,$newCode);
                }
                
                $this->model->update($this->params['ISUPDATE'],$arr);
                ajax::ship('rs','01');
            }else{
                $select = $table->select()
                                ->from($table,array('COUNT(*) as count'))
                                ->where('CODE = ?',$this->params['CODE']);
                $cnt = $table->fetchRow($select)->toArray();
                if($cnt['count'] == '0'){
                    config::addSystemRoles($arr['CODE']);
                    $this->model->insert($arr);
                    ajax::ship('rs','01');
                }else{
                    ajax::ship('rs','02');
                }
            }
            $oldCode = $this->params['CODEOLD'];
            $newCode = $this->params['CODE'];
            
            
        }else{
            ajax::ship('rs','03');
        }
        
        $select         = $table->select()
                                ->from($table,array('CODE'));
        $arrRole        = $table->fetchAll($select,0);
        $listRole = '' ;
        foreach($arrRole as $item){
            $listRole .= $item['CODE'].',';
        }
        $listRole       = rtrim($listRole,",");
        config::setSystemRoles($listRole);
    }

    public function deleteAction()
    {
        $this->model->delete($this->params['DEL']);
        $this->_redirect('/admin/roles/index');
    }
}